package com.twentytwok.hospitalsystem22K.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.twentytwok.hospitalsystem22K.common.Code;
import com.twentytwok.hospitalsystem22K.common.R;
import com.twentytwok.hospitalsystem22K.mapper.BookMapper;
import com.twentytwok.hospitalsystem22K.mapper.TimeQuantumMapper;
import com.twentytwok.hospitalsystem22K.pojo.*;
import com.twentytwok.hospitalsystem22K.service.BookService;
import com.twentytwok.hospitalsystem22K.vo.UserSelectBookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 实现类
 *
 * @author Yuanao
 * @date 2021年11月11日21:19:44
 */
@Service("bookService")
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private TimeQuantumMapper timeQuantumMapper;


    @Override
    public R getTimeQuantums() {
        List<TimeQuantum> timeQuantums = timeQuantumMapper.selectByExample(null);
        if (!timeQuantums.isEmpty()) {
            return new R(Code.OK, "ok", timeQuantums);
        }
        return new R(Code.ERROR, "查找失败");
    }

    @Override
    public R deleteBook(Integer id) {
        Book book = new Book();
        book.setId(id);
        book.setDelete(true);
        int result = bookMapper.updateByPrimaryKeySelective(book);
        if (result > 0) {
            return new R(Code.OK, "删除成功");
        } else {
            return new R(Code.ERROR, "删除失败");
        }
    }

    @Override
    public R modifyBook(Book book) {
        //如果传输过来的是空字符串,那么将空字符串转换成null
//        if (book.getPassword() != null && book.getPassword().equals("")) {
//            adminUser.setPassword(null);
//        }

        int result = bookMapper.updateByPrimaryKeySelective(book);

        if (result > 0) {
            return new R(Code.OK, "修改成功");
        }
        return new R(Code.ERROR, "修改失败");
    }

    @Override
    public R addBook(Book book) {
        book.setTime(LocalDateTime.now());  //设置预约时间是当前

        int result = bookMapper.insertSelective(book);
        if (result > 0) {
            return new R(Code.OK, "添加成功");
        } else {
            return new R(Code.ERROR, "添加失败");
        }
    }

    @Override
    public R getBook(Integer id) {
        BookExample example = new BookExample();
        example.createCriteria().andIdEqualTo(id).andDeleteEqualTo(false);  //获取id等于这个数,并且没有删除的用户

        List<Book> books = bookMapper.selectByExample(example);

        if (!books.isEmpty()) {
            return new R(Code.OK, "OK", books.get(0));
        }
        return new R(Code.ERROR, "没有找到指定用户");
    }

    @Override
    public R getBooks(Integer page, Integer limit, Integer userId, Integer doctorUserId) {

        PageHelper.startPage(page, limit);


        BookExample example = new BookExample();
        BookExample.Criteria criteria = example.createCriteria();
        criteria.andDeleteEqualTo(false);   //必须是没有被删除的

        //当name传过来有值的时候开启模糊查询
        if (userId != null && userId != 0) {
            criteria.andUserIdEqualTo(userId);
        }
        //当name传过来有值的时候开启模糊查询
        if (doctorUserId != null && doctorUserId != 0) {
            criteria.andDoctorUserIdEqualTo(doctorUserId);
        }

        List<Book> books = bookMapper.selectByExample(example);

        PageInfo<Book> pageInfo = new PageInfo<>(books);

        return new R(Code.OK, "OK", (int) pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public Integer getTodayBookCountByTimeQuantumId(Integer doctorId,LocalDate date, Integer timeQuantum) {

        BookExample example = new BookExample();
        example.createCriteria()
                .andDoctorUserIdEqualTo(doctorId)
                .andDateEqualTo(date)
                .andTimeQuantumIdEqualTo(timeQuantum)
                .andDateEqualTo(LocalDate.now());

        List<Book> books = bookMapper.selectByExample(example);

        //返回数量
        return books.size();
    }

    @Override
    public R submitBook(Book book) {
        book.setTime(LocalDateTime.now());

        int result = bookMapper.insertSelectiveSelectKey(book);

        if (result > 0) {
            return new R(Code.OK, "预约成功", book);
        } else {
            return new R(Code.ERROR, "预约失败");
        }
    }

    @Override
    public R getUserSelectBook(Integer id) {
        List<UserSelectBookVo> userSelectBookVos =  bookMapper.getUserSelectBook(id);

        if (!userSelectBookVos.isEmpty()) {
            return new R(Code.OK,"OK",userSelectBookVos);
        } else {
            return new R(Code.ERROR,"没有预约信息");
        }
    }
}
